<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" scroll="no">
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="ef" uri="control"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>${Title}</title>
<link rel="shortcut icon" href="${res}favicon.ico" type="image/x-icon" />
 <!--Basic Styles-->
<link href="../framework/css/font.css" rel="stylesheet" />
<link href="../framework/miniui/themes/material/miniui.css" rel="stylesheet" />
<link href="../framework/css/app.min.1.css" rel="stylesheet" type="text/css" />
<link href="../framework/css/app.min.2.css" rel="stylesheet" type="text/css" />
<script src="../framework/main.js" type="text/javascript"></script>
<style type="text/css">
    html, body{margin:0;padding:0;border:0;width:100%;height:100%;}
    label{font-weight:normal;}
    b{font-style: normal;}
    </style>
</head>
<body class="body-list">
<script>
//保存
function save(){
 	var dc = {};
	if(!$V('#ID')){
		top.mini.alert("@{Framework.DataGrid.PleaseSelectFirst}");
	}
	//清除置灰的权限,置灰权限为角色权限所有
	$("input.disabled").each(function(i,data){
		delete PrivMap[data.id];
	});		
	dc.ID=$('#ID').val();
	dc.Type=$('#Type').val();
 	dc.Data=PrivMap;
	Server.sendRequest("MemberMenuPriv.save", dc, function(response) {
		if(response.Status == 1){
			top.mini.showTips({content:response.Message,state:'success'});
		}else{
			top.mini.warn(response.Message);
		}
	});
}

//选中全部
function onSelectAllClick() {
	var f = mini.get('AllSelect').getChecked();
	$N("Menus").each(function(ele){
		if(!ele.disabled){
			ele.checked = f;
			PrivMap[ele.value] = f?1:0;
		}
	});
	$N("Privs").each(function(ele){
		if(!ele.disabled){
			ele.checked = f;
			PrivMap[ele.value] = f?1:0;
		}
	});
}

function checkPriv(){
	$('#menuPriv_form1').enable();
	mini.get('menuPriv_toolbar1').enable();
	<ef:if condition="${Type=='B'}">
	if(!Application.hasPriv("Platform.Branch.SetPrivRange")){
		$('#menuPriv_form1').disable();
		$('#menuPriv_toolbar1').disable();
	}
	</ef:if>
	<ef:if condition="${Type=='U'}">
	if(!Application.hasPriv("Platform.User.SetPriv")){
		$('#menuPriv_form1').disable();
		$('#menuPriv_toolbar1').disable();
	}
	</ef:if>
	<ef:if condition="${Type=='R'}">
	if(!Application.hasPriv("Platform.Role.SetPriv")){
		$('#menuPriv_form1').disable();
		$('#menuPriv_toolbar1').disable();
	}
	</ef:if>
}

//将不允许选中的单选框灰掉
function setUncheckable(){
	var arr = $T("input");
	for(var i=0;i<arr.length;i++){
		if(arr[i].type=='checkbox'){
			var v = arr[i].value;
			if(_FullPrivFlag||UncheckablePriv.hasPriv(v)){
				arr[i].checked = true;
 				$(arr[i]).disable();
			}
		}
	}
	if(!$V("ID")){
		$('#menuPriv_trToolbar a').disable();
		for(var i=0;i<arr.length;i++){
			$(arr[i]).disable();
		}
	}
}

function onMenuClick(cb){
	var menuID = cb.value;
	setOneMenuStatus(cb);
	//上级菜单
	var parentID = cb.getAttribute("parentID");
	while(parentID&&$G(parentID)&&cb.checked){
		if(!$G(parentID).checked){
			$G(parentID).checked = true;
			PrivMap[parentID] = 1;
		}
		parentID = $G(parentID).getAttribute("parentID");
	}
	//下级菜单
	$N("Menus").each(function(ele){
		if(ele.getAttribute("parentID")==menuID){
			ele.checked = cb.checked;
			setOneMenuStatus(ele);
		}	
	});
}

function setOneMenuStatus(cb){
	var menuID = cb.value;
	PrivMap[menuID] = cb.checked?1:0;
	//权限项
	$N("Privs").each(function(ele){
		if(ele.getAttribute("parentMenuID") == menuID){
			if(!ele.disabled){
				ele.checked = cb.checked;
				PrivMap[ele.value] = cb.checked?1:0;
			}
		}
	});	
}

function onPrivItemClick(cb){
	var id = cb.value;
	PrivMap[id] = cb.checked?1:0;
	var menuID = id.substring(0,id.lastIndexOf("."));
	var menu = $G(menuID);
	while(menuID.indexOf(".")!=-1&&menu==null){
		menuID = menuID.substring(0,menuID.lastIndexOf("."));
		menu = $G(menuID);
	}
	if(cb.checked&&!menu.checked){
		menu.checked = true;
		PrivMap[menu.value] = 1;
		var parentID = menu.getAttribute("parentID");
		while(parentID&&$G(parentID)){
			if(!$G(parentID).checked){
				$G(parentID).checked = true;
				PrivMap[parentID] = 1;
			}
			parentID = $G(parentID).getAttribute("parentID");
		}
	}
}
function hideLockContent(){
	var checkbox=document.getElementById('ContentCore.EditWorkSpace.LockedContent');
	if(checkbox){
		$(checkbox).parent().hide();
	}
}
Page.onReady(checkPriv);
Page.onReady(setUncheckable);
//处理锁定内容列表页的权限不显示
Page.onLoad(hideLockContent);
</script>

<form id="menuPriv_form1">

<script>
 var PrivMap = {};
var _FullPrivFlag = false;//当前编辑对象是否可以拥有全部权限
${(PrivScript)}
</script>
<input class="mini-hidden" id="Type" value="${Type}">
<input class="mini-hidden" id="ID" value="${ID}">


<div class="mini-toolbar" plain="true" id="menuPriv_toolbar1">
    <a class="mini-button" id="ButtonSave" iconCls="icon-save" plain="true" onclick="save();"> 保存</a>
    <a class="mini-button" id="AllSelect" iconCls="icon-check-all" plain="true" onClick="onSelectAllClick()" checkOnClick="true">全选</a>
</div>
<div class="mini-scroll">
	<div style="padding:10px;">
		<ef:list id="list1" method="MenuPriv.items">
			<div style="padding-top:4px;<ef:if condition='${ParentFlag}' out='padding-left:20px' />">
				<div class="z-legend">
				<input type="checkbox" ${Checked} name="Menus" id="${ID}" value="${ID}" parentID="${ParentID}" onclick="onMenuClick(this)" /><label for="${ID}"><i class="iconfont ${IconCls}">&nbsp;<b>${Name}</b></i></label></div>
				<div style=" padding:5px 0;">
				<ef:list id="list2" method="MenuPriv.bindPrivItemList">
					<span style="padding:0 10px;">
						<label for='${Item}'><input onclick='onPrivItemClick(this)' type='checkbox' ${Checked} id='${Item}' name='Privs' value='${Item}' parentMenuID="${ID}">
						${Name}</label>
					</span>
				</ef:list>
				<ef:if condition="${PrivMemo}">
					<span style='padding:10px;padding-left:20px'><i class="iconfont icon-conowcry">&nbsp;<b>${PrivMemo}</b></i></span>
				</ef:if>
				</div>
			</div>
 		</ef:list>
	</div>
</div>
</form>
</body>
</html>
